/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player; 
    int a=0;
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room outside, theater, pub, lab, office, lecture,land;
      
        // create the rooms
        outside = new Room("搬砖公司门口 'east进入大厅,west进入一号工地，south进入小茶馆'");
        theater = new Room("公司大厅 ‘在这里你可以捡到补充能能量的东西’", new Thing("小熊饼干",20));
        pub = new Room("一号工地",new Thing("砖头",-5));
        lab = new Room("小茶馆", new Thing("小熊饼干",20));
        office = new Room("搬砖公司办公室‘east’",new Thing("砖头",-5));
        lecture = new Room("二号工地",new Thing("砖头",-5));
        land = new Room("待开发地块");
        
        // initialise room exits
        //outside.setExits(null, theater, lab, pub, null, null);
        outside.setExit("east",theater);
        outside.setExit("south",lab);
        outside.setExit("west",pub);
        //theater.setExits(null, null, null, outside, null, null);
        theater.setExit("west",outside);
        theater.setExit("up",lecture);
        theater.setExit("down",office);
        //lecture.setExits(null, null, null, null, null,theater);
        lecture.setExit("down",theater);
        //pub.setExits(null, outside, null, null, null, null);
        pub.setExit("east",outside);
        pub.setExit("south",land);
        //lab.setExits(outside, office, null, null, null, null);
        lab.setExit("north",outside);
        lab.setExit("east",office);
        lab.setExit("west",land);
        //office.setExits(null, null, null, lab, null, null);
        office.setExit("west",lab);
        office.setExit("north",theater);
        land.setExit("east",lab);
        currentRoom = outside;  // start game outside
    }

    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
            boolean exit=false;
           // exit=player.over();
            if(exit==true){
              System.out.println("你的能量已经不足了，要去休息休息了。");
              System.exit(1);
            }
            finished = player.gameover();
        }

        
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("欢迎来到史上最菜搬砖世界!");
        System.out.println("这是一个只能搬砖的世界，连开发者不知道是什么样的游戏.");
        System.out.println("当然中途你可能能够遇到不一样的东西");
        System.out.println("如果你需要帮助请输入 'help'.");
        System.out.println();
        System.out.println("你现在处于" + currentRoom.getDescription());
        System.out.print("Exits: ");
        System.out.println();
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("你输入的语句是没有用的...");
            System.out.println("请重新输入好嘛？");
            return false;
        }

        Word commandWord = command.getCommandWord();
        switch(commandWord){
           case HELP:
              printHelp();
              break;
           case GO:
              goRoom(command);
              player.step();
              break;
           case QUIT:
              wantToQuit=quit(command);
              break;
           case PICK:
              pick();
              break;
           case LOOK:
              look();
              break;
           case EAT:
              eat(command);
              break;
           //case CHECK:
             // check();
             // break;
        }
        //if (commandWord.equals("help")) {
          //  printHelp();
        //}
        //else if (commandWord.equals("go")) {
          //  goRoom(command);
        //}
        //else if (commandWord.equals("quit")) {
          //  wantToQuit = quit(command);
        //}
        //else if (commandWord.equals("quit")){
          //  wantToQuit = quit(command);
        //}
        //else if (commandWord.equals("pick")){
          //  pick(); 
        //}
        return wantToQuit;
    }
    // implementations of user commands:
    
    private void look(){
        System.out.println(currentRoom.getThing()); 
    }
    
    private void pick()
    {
       Thing thing = currentRoom.getThings();
       if(thing != null){
             player.pick(currentRoom.getThings());
             System.out.println("你捡到了一个" + thing.getName() + ".");
            if(thing.getName()=="砖头"){
                a++;
                System.out.println(player.getminusStrength());
            }
            
            if(a>=3){
                System.out.println("你也太强了吧，附近工地的砖头已经搬空了，等待它的生产吧！");
                //System.out.println();
                System.exit(1);
            }
        }else{
        System.out.println("你捡没捡到东西自己还不清楚嘛？？");
        }
    }
    
    private void eat(Command command){
       if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("你要吃掉什么？");
            return;
        }else{
            String food = command.getSecondWord();
            player.eat(food);
        }
    }
    
    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("啊？不是吧阿sir，这就不行了吗？这就需要帮助了吗？");
        System.out.println("真的吗真的吗真的是这样子吗？");
        System.out.println("搬砖都不行了吗？");
        System.out.println("实在不行就...");
        System.out.println("那就输入：");
        System.out.println("放弃吧!嘻嘻嘻^_^");
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("你想往哪边走？");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goRoom(direction);
        //if(direction.equals("north")) {
        //    nextRoom = currentRoom.northExit;
        //}
        //if(direction.equals("east")) {
           // nextRoom = currentRoom.eastExit;
        //}
        //if(direction.equals("south")) {
            //nextRoom = currentRoom.southExit;
        //}
        //if(direction.equals("west")) {
           // nextRoom = currentRoom.westExit;
        //}
        //if(direction.equals("up")) {
           // nextRoom = currentRoom.upExit;
        //}
        //if(direction.equals("down")) {
           // nextRoom = currentRoom.downExit;
        //}
        
        if (nextRoom == null) {
            System.out.println("这块工地没有砖头哦，再走去其他的试试吧");
        }
        else {
            currentRoom = nextRoom;
            System.out.println("你现在处于 " + currentRoom.getDescription());
            currentRoom.printExits();
            //System.out.print("Exits: "); 
            //if(currentRoom.northExit != null) {
                //System.out.print("north");
            //}
            //if(currentRoom.eastExit != null) {
               // System.out.print("east");
            //}
            //if(currentRoom.southExit != null) {
              //  System.out.print("south");
            //}
            //if(currentRoom.westExit != null) {
              //  System.out.print("west");
            //}
            //if(currentRoom.upExit != null) {
              //  System.out.print("up");
            //}
            //if(currentRoom.downExit != null) {
               // System.out.print("down");
            //}
            //System.out.println();
        }
    }

    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("丢掉哪块砖头呢?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
}
